﻿using CPPEI.Coolzon.ServiceProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// WaitingView.xaml 的交互逻辑
    /// </summary>
    public partial class WaitingView : View
    {
        public string SelectedSolutionName { get; private set; }
        public string ViewStateToken { get; private set; }
        public WaitingView()
        {
            InitializeComponent();
            InitImage();
        }

        private void InitImage()
        {
            waitingImage.AnimatinoImageControl(AppDomain.CurrentDomain.BaseDirectory + "WaitingView/zj.gif");
        }

        protected override void OnActive()
        {
            Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Register(this);

            // 构造获取控制域列表的请求
            Service.Contract.ReqGetDomainListData requestData = new Service.Contract.ReqGetDomainListData();
            requestData.SolutionName = "";

            CPPEI.Coolzon.ServiceProxy.MainServiceRef.Request request = RequestUtil.BuildRequest(
                UIContext.Current.ControlDomainId,
                UIContext.Current.UserIdentity,
                Service.Contract.REQUEST_SCENARIO.CONNENT,
                Service.Contract.REQUEST_COMMAND.CONNECT.GETDOMAINLIST,
                Service.Contract.REQUEST_MODE.SELF,
                requestData);
            ClientProxyManager.Instance.SendRequestSys(request);

            base.OnActive();
        }

        [Common.Subscribe]
        public void OnReceiveGetDomainListData(Service.Contract.RespGetDomainListData data)
        {
            if (data != null && data.Domains != null && data.Domains.Length > 0)
            {
                this.Dispatcher.Invoke(() =>
                {
                    // 构造进入控制域的请求
                    Service.Contract.ReqEnterDomainData requestData = new Service.Contract.ReqEnterDomainData();
                    requestData.DomainId = data.Domains[0].Id;
                    requestData.SolutionName = data.Domains[0].SolutionName;

                    CPPEI.Coolzon.ServiceProxy.MainServiceRef.Request request = RequestUtil.BuildRequest(
                        UIContext.Current.ControlDomainId,
                        UIContext.Current.UserIdentity,
                        Service.Contract.REQUEST_SCENARIO.CONNENT,
                        Service.Contract.REQUEST_COMMAND.CONNECT.ENTERDOMAIN,
                        Service.Contract.REQUEST_MODE.SELF,
                        requestData);
                    ClientProxyManager.Instance.SendRequestSys(request);
                });
            }
        }

        [Common.Subscribe]
        public void OnReceiveEnterDomainData(Service.Contract.RespEnterDomainData data)
        {
            if (data != null)
            {
                if (data.IsSuccess)
                {
                    UIContext.Current.ControlDomainId = data.DomainId;
                    UIContext.Current.OperatorIdentity = data.OperatorIdentity;
                    //UIContext.Current.IsNeesSyncViewStateWhenEnterDomain = data.IsNeedSyncViewState;
                    SelectedSolutionName = data.SolutionName;
                    ViewStateToken = data.ViewStateToken;
                    this.Dispatcher.Invoke(() =>
                    {
                        Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(this);
                        this.Submit();
                    });
                }
                else
                {
                    MessageBox.Show(data.Message);
                }
            }
        }
    }
}
